package com.dd.admin.business.sell.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.sell.domain.CancelSellDto;
import com.dd.admin.business.sell.enums.CancelSellServiceEnum;
import com.dd.admin.business.sell.service.CancelSellService;
import com.dd.admin.business.sellDetail.service.SellDetailService;
import com.dd.admin.common.aop.operationLog.aop.OperLog;
import com.dd.admin.common.aop.operationLog.aop.OperType;
import com.dd.admin.common.exception.ApiException;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.business.sell.domain.SellVo;
import com.dd.admin.business.sell.domain.SellDto;
import com.dd.admin.business.sell.service.SellService;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

import static com.dd.admin.business.pay.enmus.CashierExceptionEnum.PAY_SELL_CANCEL_ERROR;
import static com.dd.admin.business.pay.enmus.PayConst.SELL_STATUS_REVOCATION;
import static com.dd.admin.common.consts.MemberShopConst.SELL_STATUS_CANCEL;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-19
 */
@Api(tags = "订单")
@RestController
public class SellController {

    @Autowired
    SellService sellService;

    @Autowired
    Map<String, CancelSellService> cancelSellServiceMap;

    @Autowired
    SellDetailService sellDetailService;

    @ApiOperation(value = "订单-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/sell/page")
    @OperLog(operModule = "获取订单列表",operType = OperType.QUERY,operDesc = "获取订单列表")
    public ResultBean<IPage<SellVo>> page(SellDto sellDto) {
        sellDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<SellVo> pageInfo = sellService.selectSellPage(sellDto);
        sellService.setSellDetail(pageInfo.getRecords());
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "订单-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/sell/list")
    public ResultBean<List<SellVo>> list(SellDto sellDto) {
        sellDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<SellVo> list = sellService.selectSellList(sellDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "订单-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/sell/add")
    public ResultBean<Sell> add(@RequestBody @Validated SellDto sellDto) {
        Sell sell = BeanUtil.copyProperties(sellDto, Sell.class);
        sellService.save(sell);
        return ResultBean.success(sell);
    }

    @ApiOperation(value = "订单-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/sell/{sellId}")
    public ResultBean<SellVo> get(@PathVariable @NotBlank String sellId) {
        Sell sell = sellService.getById(sellId);
        SellVo sellVo = BeanUtil.copyProperties(sell,SellVo.class);
        return ResultBean.success(sellVo);
    }

    @ApiOperation(value = "订单-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/sell/update")
    public ResultBean<Sell> update(@RequestBody @Validated(UpdateGroup.class) SellDto sellDto) {
        Sell sell = BeanUtil.copyProperties(sellDto, Sell.class);
        sellService.updateById(sell);
        return ResultBean.success(sell);
    }

    @ApiOperation(value = "订单-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/sell/delete/{sellId}")
    public ResultBean<Sell> delete(@PathVariable @NotBlank String sellId) {
        Boolean b = sellService.removeById(sellId);
        return ResultBean.success(b);
    }

    @ApiOperation(value = "订单-销单")
    @ApiOperationSupport(order = 6)
    @PostMapping("/admin/sell/cancel")
    @Transactional
    public ResultBean<Sell> cancel(@RequestBody @Validated CancelSellDto cancelSellDto) {
        Sell sell = sellService.getById(cancelSellDto.getSellId());
        if(sell.getSellStatus().equals(SELL_STATUS_CANCEL)){
            throw new ApiException(PAY_SELL_CANCEL_ERROR);
        }
        //销单
        cancelSellServiceMap.get(CancelSellServiceEnum.getEnumBySellType(sell.getSellType()).getServiceName()).cancel(sell);
        sell.setSellStatus(SELL_STATUS_REVOCATION);
        sell.setRemark(cancelSellDto.getRemark());
        sellService.updateById(sell);
        sellDetailService.updateSellDetailStatus(sell.getSellId());
        return ResultBean.success();
    }

}
